#! /usr/bin/python

import time

t1 = time.time()

fTable=[1,1]

for i in range(2, 10):
    fTable.append(fTable[i-1]*i)

print fTable[9]*6
chainTable=[0, 1]
for i in range(2, 2177281):
    t = i
    s = 0
    while(t):          
         s += fTable[t%10]
         t/=10
    chainTable.append(s)
print len(chainTable)

def chainLen(n):
    org = n
    ll =[n]
    while(1):
         if (ll.count(n) > 1): 
		return len(ll)-1
         n = chainTable[n]
	 ll.append(n)

print chainLen(69)
count = 0
chainLenTable = []
for i in range(540, 1000000):
    chainLenTable[i] = chainLen(i)
    if chainLen(i) == 60: count+=1

print count, time.time()-t1    
